﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;
using YJKArchMethodLibrary.Utils;

namespace CarRampCreateAndEdit
{
	public partial class CreateRampForm : System.Windows.Forms.Form
	{
		public CreateRampForm(ExternalCommandData cmdData, List<string> boardStyles, List<string> levelNames, List<double> levelElevations, List<SegmentData> segmentDataList)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
			this.m_lstBoardStyles = boardStyles;
			this.m_lstLevelNames = levelNames;
			this.m_lstLevelNames.Reverse();
			this.m_lstLevelElevations = levelElevations;
			this.m_lstLevelElevations.Reverse();
			this.m_lstSegmentData = segmentDataList;
			this.m_MainPreviewer = new Previewer(this, this.panelMain.Left, this.panelMain.Top, this.panelMain.Width, this.panelMain.Height, 1u);
			base.MouseWheel += this.m_MainPreviewer.MouseWheel;
			this.m_SecotionPreviewer = new Previewer(this, this.panelSecond.Left, this.panelSecond.Top, this.panelSecond.Width, this.panelSecond.Height, 2u);
			base.MouseWheel += this.m_SecotionPreviewer.MouseWheel;
		}

		public int BoradStyleIndex
		{
			get
			{
				return this.cmbBoardStyle.SelectedIndex;
			}
		}

		public List<SegmentData> SegmentDataList
		{
			get
			{
				return this.m_lstSegmentData;
			}
		}

		public void SetFloorType(string floorTypeName)
		{
			this.m_strOldFloorTypeName = floorTypeName;
		}

		private void GetTopBottomElevation(ref double topElevation, ref double bottomElevation)
		{
			List<double> list = new List<double>();
			foreach (SegmentData segmentData in this.m_lstSegmentData)
			{
				list.Add(segmentData.StartElevation);
				list.Add(segmentData.EndElevation);
			}
			list.Sort();
			bottomElevation = list[0];
			topElevation = list[list.Count - 1];
		}

		private void GetLeftRightWidth(ref double leftWidth, ref double rightWidth)
		{
			leftWidth = this.m_lstSegmentData[0].LeftWidth;
			rightWidth = this.m_lstSegmentData[0].RightWidth;
		}

		private void CalculateListView()
		{
			if (this.dgvSegmentSInfo.Rows.Count > 0)
			{
				this.dgvSegmentSInfo.Rows.Clear();
			}
			for (int i = 0; i < this.m_lstSegmentData.Count; i++)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				this.AddTextCellToRow((i + 1).ToString(), ref dataGridViewRow);
				SegmentData segmentData = this.m_lstSegmentData[i];
				double num = segmentData.Length();
				double startElevation = segmentData.StartElevation;
				string value = (startElevation / 1000.0).ToString("f3");
				this.AddTextCellToRow(value, ref dataGridViewRow);
				double endElevation = segmentData.EndElevation;
				string value2 = (endElevation / 1000.0).ToString("f3");
				this.AddTextCellToRow(value2, ref dataGridViewRow);
				string value3 = segmentData.LeftWidth.ToString("f0");
				this.AddTextCellToRow(value3, ref dataGridViewRow);
				string value4 = segmentData.RightWidth.ToString("f0");
				this.AddTextCellToRow(value4, ref dataGridViewRow);
				string value5 = AssistFunc.feetToMM(num).ToString("f0");
				this.AddTextCellToRow(value5, ref dataGridViewRow);
				double num2 = Math.Abs(segmentData.EndElevation - segmentData.StartElevation);
				string value6 = string.Empty;
				if (this.m_bScaleSlope)
				{
					value6 = "1:" + (AssistFunc.feetToMM(num) / num2).ToString("f2");
				}
				else
				{
					value6 = (100.0 * num2 / AssistFunc.feetToMM(num)).ToString("f2");
				}
				this.AddTextCellToRow(value6, ref dataGridViewRow);
				string value7 = "直线";
				string value8 = "--";
				string value9 = "--";
				string value10 = "1";
				double num3 = 0.0;
				if (segmentData.Radius(ref num3))
				{
					value7 = "弧线";
					value8 = AssistFunc.feetToMM(num3).ToString("f0");
					value9 = (num / num3 * 180.0 / Math.PI).ToString("f2");
				}
				this.AddTextCellToRow(value7, ref dataGridViewRow);
				this.AddTextCellToRow(value8, ref dataGridViewRow);
				this.AddTextCellToRow(value9, ref dataGridViewRow);
				this.AddTextCellToRow(value10, ref dataGridViewRow);
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell = new DataGridViewCheckBoxCell();
				dataGridViewCheckBoxCell.Value = (segmentData.VertArcRadius > 0.0);
				dataGridViewRow.Cells.Add(dataGridViewCheckBoxCell);
				this.dgvSegmentSInfo.Rows.Add(dataGridViewRow);
			}
			this.UpdateRowsBackColor();
		}

		private void ReCalculateSlope()
		{
			if (this.dgvSegmentSInfo.Rows.Count != this.m_lstSegmentData.Count)
			{
				return;
			}
			for (int i = 0; i < this.dgvSegmentSInfo.Rows.Count; i++)
			{
				SegmentData segmentData = this.m_lstSegmentData[i];
				double val = segmentData.Length();
				double num = Math.Abs(segmentData.EndElevation - segmentData.StartElevation);
				string value = string.Empty;
				if (this.m_bScaleSlope)
				{
					value = "1:" + (AssistFunc.feetToMM(val) / num).ToString("f2");
				}
				else
				{
					value = (100.0 * num / AssistFunc.feetToMM(val)).ToString("f2");
				}
				DataGridViewRow dataGridViewRow = this.dgvSegmentSInfo.Rows[i];
				DataGridViewTextBoxCell dataGridViewTextBoxCell = dataGridViewRow.Cells[6] as DataGridViewTextBoxCell;
				if (dataGridViewTextBoxCell != null)
				{
					dataGridViewTextBoxCell.Value = value;
				}
			}
			this.UpdateRowsBackColor();
		}

		private void UpdateRowsBackColor()
		{
			if (this.dgvSegmentSInfo == null)
			{
				return;
			}
			if (this.dgvSegmentSInfo.Rows.Count < 1)
			{
				return;
			}
            System.Drawing.Color color = System.Drawing.Color.Gainsboro;
			for (int i = 0; i < this.m_lstSegmentData.Count; i++)
			{
				if (i > 0 && !SegmentData.CanBeMerged(this.m_lstSegmentData[i - 1], this.m_lstSegmentData[i]))
				{
                    if (color == System.Drawing.Color.White)
					{
                        color = System.Drawing.Color.Gainsboro;
					}
					else
					{
                        color = System.Drawing.Color.White;
					}
				}
				this.dgvSegmentSInfo.Rows[i].DefaultCellStyle.BackColor = color;
			}
		}

		private void AddTextCellToRow(string value, ref DataGridViewRow row)
		{
			DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
			dataGridViewTextBoxCell.Value = value;
			row.Cells.Add(dataGridViewTextBoxCell);
		}

		private string GetElevationByLevelIndex(int index)
		{
			return this.m_lstLevelElevations[index].ToString("f3");
		}

		private int FindLevelByElevation(double elevation)
		{
			double val = double.MaxValue;
			int i = 0;
			int result = 0;
			while (i < this.m_lstLevelElevations.Count)
			{
				double num = this.m_lstLevelElevations[i];
				double num2 = Math.Abs(elevation - num * 1000.0);
				if (Geometry.LessThan(num2, val))
				{
					val = num2;
					result = i;
				}
				i++;
			}
			return result;
		}

		private void tbTopElevation_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_bInitialized)
			{
				return;
			}
			string strValue = this.ValidatingMinusSign(this.tbTopElevation.Text);
			double num = 0.0;
			if (!this.ValidatingDouble(strValue, CreateRampForm.ValidatingType.LevelValue, ref num))
			{
                ((System.Windows.Forms.TextBox)sender).Text = "4.000";
				num = 4.0;
			}
			this.CalculateElevation();
			this.tbTopElevation.Focus();
		}

		private void tbBottomElevation_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_bInitialized)
			{
				return;
			}
			string strValue = this.ValidatingMinusSign(this.tbBottomElevation.Text);
			double num = 0.0;
			if (!this.ValidatingDouble(strValue, CreateRampForm.ValidatingType.LevelValue, ref num))
			{
                ((System.Windows.Forms.TextBox)sender).Text = "0.000";
				num = 0.0;
			}
			this.CalculateElevation();
			this.tbBottomElevation.Focus();
		}

		private string ValidatingMinusSign(string value)
		{
			if (value.CompareTo("-") == 0)
			{
				return "0";
			}
			return value;
		}

		private void cmbBottomLevel_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.m_bInitialized)
			{
				return;
			}
			int selectedIndex = this.cmbBottomLevel.SelectedIndex;
			this.tbBottomElevation.Text = this.GetElevationByLevelIndex(selectedIndex);
			this.CalculateListView();
		}

		private void cmbTopLevel_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.m_bInitialized)
			{
				return;
			}
			int selectedIndex = this.cmbTopLevel.SelectedIndex;
			this.tbTopElevation.Text = this.GetElevationByLevelIndex(selectedIndex);
			this.CalculateListView();
		}

		private void rbBottomToTop_CheckedChanged(object sender, EventArgs e)
		{
			if (!this.m_bInitialized)
			{
				return;
			}
			this.CalculateListView();
		}

		private void tbLeftWidth_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_bInitialized)
			{
				return;
			}
			string text = this.tbLeftWidth.Text;
			double num = 0.0;
			if (!this.ValidatingDouble(text, CreateRampForm.ValidatingType.WidthValue, ref num))
			{
                ((System.Windows.Forms.TextBox)sender).Text = "1000";
				num = 1000.0;
			}
			foreach (SegmentData segmentData in this.m_lstSegmentData)
			{
				segmentData.LeftWidth = num;
			}
			this.m_dLeftWidth = num;
			if (this.chbSmoothTransition.Checked)
			{
				this.RemoveSmoothTransition();
				this.AddSmoothTransition();
			}
			this.CalculateListView();
			this.Refresh();
			this.tbLeftWidth.Focus();
		}

		private void tbRightWidth_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_bInitialized)
			{
				return;
			}
			string text = this.tbRightWidth.Text;
			double num = 0.0;
			if (!this.ValidatingDouble(text, CreateRampForm.ValidatingType.WidthValue, ref num))
			{
                ((System.Windows.Forms.TextBox)sender).Text = "1000";
				num = 1000.0;
			}
			foreach (SegmentData segmentData in this.m_lstSegmentData)
			{
				segmentData.RightWidth = num;
			}
			this.m_dRightWidth = num;
			if (this.chbSmoothTransition.Checked)
			{
				this.RemoveSmoothTransition();
				this.AddSmoothTransition();
			}
			this.CalculateListView();
			this.Refresh();
			this.tbRightWidth.Focus();
		}

		private void tbTransitionRadius_TextChanged(object sender, EventArgs e)
		{
			double num = this.MinRadius();
			double maxRange = this.MaxRadius();
			if (!Common.IsValidating(this.tbTransitionRadius.Text, 0.1, maxRange))
			{
				this.tbTransitionRadius.Text = num.ToString("f0");
			}
			if (this.chbSmoothTransition.Checked)
			{
				this.RemoveSmoothTransition();
				this.AddSmoothTransition();
			}
			this.CalculateListView();
			this.Refresh();
			this.tbTransitionRadius.Focus();
		}

		private void tbVertRampRadius_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_bInitialized)
			{
				return;
			}
			string text = this.tbVertRampRadius.Text;
			double vertArcRadius = 0.0;
			if (!this.ValidatingDouble(text, CreateRampForm.ValidatingType.VertRadius, ref vertArcRadius))
			{
				this.tbVertRampRadius.Text = "20000";
				vertArcRadius = 20000.0;
			}
			foreach (SegmentData segmentData in this.m_lstSegmentData)
			{
				if (segmentData.VertArcRadius > 0.01)
				{
					segmentData.VertArcRadius = vertArcRadius;
				}
			}
			this.CalculateListView();
			this.Refresh();
			this.tbVertRampRadius.Focus();
		}

		private void btnNagtive_Click(object sender, EventArgs e)
		{
			double num = 0.0;
			double num2 = 0.0;
			this.GetTopBottomElevation(ref num, ref num2);
			double num3 = num2 + (num - num2) / 2.0;
			foreach (SegmentData segmentData in this.m_lstSegmentData)
			{
				XYZ startPoint = segmentData.StartPoint;
				segmentData.StartPoint = segmentData.EndPoint;
				segmentData.EndPoint = startPoint;
				segmentData.Bulge = -segmentData.Bulge;
				double endElevation = num3 * 2.0 - segmentData.StartElevation;
				segmentData.StartElevation = num3 * 2.0 - segmentData.EndElevation;
				segmentData.EndElevation = endElevation;
				double leftWidth = segmentData.LeftWidth;
				segmentData.LeftWidth = segmentData.RightWidth;
				segmentData.RightWidth = leftWidth;
			}
			this.m_lstSegmentData.Reverse();
			this.CalculateListView();
			this.Refresh();
		}

		private void tbTopElevation_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double.Parse(this.ValidatingMinusSign(this.tbTopElevation.Text));
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个整数", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbBottomElevation_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double.Parse(this.ValidatingMinusSign(this.tbBottomElevation.Text));
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个整数", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void CalculateElevation()
		{
			double num = Convert.ToDouble(this.ValidatingMinusSign(this.tbBottomElevation.Text)) * 1000.0;
			double num2 = Convert.ToDouble(this.ValidatingMinusSign(this.tbTopElevation.Text)) * 1000.0;
			double num3 = 0.0;
			foreach (SegmentData segmentData in this.m_lstSegmentData)
			{
				num3 += segmentData.Length();
			}
			double num4 = 0.0;
			new List<SegmentData>();
			foreach (SegmentData segmentData2 in this.m_lstSegmentData)
			{
				double num5 = num + num4 / num3 * (num2 - num);
				double num6 = segmentData2.Length();
				double endElevation = num5 + num6 / num3 * (num2 - num);
				segmentData2.StartElevation = num5;
				segmentData2.EndElevation = endElevation;
				num4 += num6;
			}
			this.CalculateListView();
			this.Refresh();
		}

		private void tbLeftWidth_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double.Parse(this.tbLeftWidth.Text);
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个整数", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbRightWidth_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double.Parse(this.tbRightWidth.Text);
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个整数", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbVertRampRadius_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double.Parse(this.tbVertRampRadius.Text);
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个整数", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void dgvSegmentSInfo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			int columnIndex = this.dgvSegmentSInfo.CurrentCell.ColumnIndex;
			if (columnIndex == 1 || columnIndex == 2 || columnIndex == 3 || columnIndex == 4 || columnIndex == 10)
			{
                System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
				if (textBox != null)
				{
					textBox.TextChanged += this.DGVTextBoxTextChanged;
				}
			}
		}

		protected override void DefWndProc(ref Message m)
		{
			try
			{
				switch (m.Msg)
				{
				case 1125:
				{
					DrawingKit drawingKit = new DrawingKit(this.m_Revit);
					if (m.WParam.ToInt32() == 1)
					{
						List<CurveInfo> curves = new List<CurveInfo>();
						List<TextNoteInfo> textNotes = new List<TextNoteInfo>();
						drawingKit.CreateProfileCurves(this.m_lstSegmentData, this.MinRadius(), ref curves, ref textNotes);
						this.m_MainPreviewer.Curves = curves;
						this.m_MainPreviewer.TextNotes = textNotes;
					}
					else if (m.WParam.ToInt32() == 2)
					{
						List<CurveInfo> curves2 = new List<CurveInfo>();
						List<TextNoteInfo> textNotes2 = new List<TextNoteInfo>();
						drawingKit.CreateSecotionCurves(this.m_lstSegmentData, ref curves2, ref textNotes2);
						this.m_SecotionPreviewer.Curves = curves2;
						this.m_SecotionPreviewer.TextNotes = textNotes2;
					}
					break;
				}
				case 1126:
					this.CalculateListView();
					break;
				default:
					base.DefWndProc(ref m);
					break;
				}
			}
			catch (Exception)
			{
				base.DefWndProc(ref m);
			}
		}

		private bool ValidatingDouble(string strValue, CreateRampForm.ValidatingType validatingType, ref double dValue)
		{
			double num = 0.0;
			try
			{
				num = Convert.ToDouble(strValue);
			}
			catch (Exception)
			{
				return false;
			}
			if (validatingType == CreateRampForm.ValidatingType.GreaterThanZero && Geometry.GreaterThan(num, 0.0))
			{
				dValue = num;
				return true;
			}
			if (validatingType == CreateRampForm.ValidatingType.GreaterThanAndEqualZero && Geometry.Greaterthan_Or_Equal(num, 0.0))
			{
				dValue = num;
				return true;
			}
			if (validatingType == CreateRampForm.ValidatingType.None)
			{
				dValue = num;
				return true;
			}
			if (validatingType == CreateRampForm.ValidatingType.WidthValue)
			{
				if (Geometry.Greaterthan_Or_Equal(num, 1.0) && Geometry.Lessthan_Or_Equal(num, 5000.0))
				{
					dValue = num;
					return true;
				}
				MessageBox.Show("宽度数值范围：1～5000mm", "盈建科提示");
			}
			else if (validatingType == CreateRampForm.ValidatingType.LevelValue)
			{
				if (Geometry.Lessthan_Or_Equal(Math.Abs(num), 1000.0))
				{
					dValue = num;
					return true;
				}
				MessageBox.Show("标高数值范围：-1000～1000m", "盈建科提示");
			}
			else if (validatingType == CreateRampForm.ValidatingType.VertRadius)
			{
				if (num >= 20000.0)
				{
					dValue = num;
					return true;
				}
				MessageBox.Show("缓坡半径应大于20000mm", "盈建科提示");
			}
			return false;
		}

		[DllImport("user32.dll", EntryPoint = "PostMessageA")]
		private static extern bool PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dgvSegmentSInfo.CurrentCell.RowIndex;
            string text = ((System.Windows.Forms.TextBox)sender).Text;
			double num = 0.0;
			switch (this.dgvSegmentSInfo.CurrentCell.ColumnIndex)
			{
			case 1:
				if (this.ValidatingDouble(this.ValidatingMinusSign(text), CreateRampForm.ValidatingType.LevelValue, ref num))
				{
					if (!Geometry.IsEqual(this.m_lstSegmentData[rowIndex].StartElevation, num * 1000.0))
					{
						this.m_lstSegmentData[rowIndex].StartElevation = num * 1000.0;
						this.UpdateRowGradient(rowIndex);
						if (rowIndex > 0)
						{
							this.m_lstSegmentData[rowIndex - 1].EndElevation = num * 1000.0;
							this.dgvSegmentSInfo.Rows[rowIndex - 1].Cells[2].Value = num;
							this.UpdateRowGradient(rowIndex - 1);
							this.UpdateRowsBackColor();
						}
					}
				}
				else
				{
                    ((System.Windows.Forms.TextBox)sender).Text = (this.m_lstSegmentData[rowIndex].StartElevation / 1000.0).ToString("f3");
				}
				break;
			case 2:
				if (this.ValidatingDouble(this.ValidatingMinusSign(text), CreateRampForm.ValidatingType.LevelValue, ref num))
				{
					if (!Geometry.IsEqual(this.m_lstSegmentData[rowIndex].EndElevation, num * 1000.0))
					{
						this.m_lstSegmentData[rowIndex].EndElevation = num * 1000.0;
						this.UpdateRowGradient(rowIndex);
						if (rowIndex < this.m_lstSegmentData.Count - 1)
						{
							this.m_lstSegmentData[rowIndex + 1].StartElevation = num * 1000.0;
							this.dgvSegmentSInfo.Rows[rowIndex + 1].Cells[1].Value = num;
							this.UpdateRowGradient(rowIndex + 1);
							this.UpdateRowsBackColor();
						}
					}
				}
				else
				{
                    ((System.Windows.Forms.TextBox)sender).Text = (this.m_lstSegmentData[rowIndex].EndElevation / 1000.0).ToString("f3");
				}
				break;
			case 3:
				if (this.ValidatingDouble(text, CreateRampForm.ValidatingType.WidthValue, ref num))
				{
					if (!Geometry.IsEqual(this.m_lstSegmentData[rowIndex].LeftWidth, num))
					{
						this.m_lstSegmentData[rowIndex].LeftWidth = num;
						if (this.chbSmoothTransition.Checked)
						{
							this.RemoveSmoothTransition();
							this.AddSmoothTransition();
							this.CalculateListView();
						}
						this.UpdateRowsBackColor();
					}
				}
				else
				{
                    ((System.Windows.Forms.TextBox)sender).Text = this.m_lstSegmentData[rowIndex].LeftWidth.ToString("f0");
				}
				break;
			case 4:
				if (this.ValidatingDouble(text, CreateRampForm.ValidatingType.WidthValue, ref num))
				{
					if (!Geometry.IsEqual(this.m_lstSegmentData[rowIndex].RightWidth, num))
					{
						this.m_lstSegmentData[rowIndex].RightWidth = num;
						if (this.chbSmoothTransition.Checked)
						{
							this.RemoveSmoothTransition();
							this.AddSmoothTransition();
							this.CalculateListView();
						}
						this.UpdateRowsBackColor();
					}
				}
				else
				{
                    ((System.Windows.Forms.TextBox)sender).Text = this.m_lstSegmentData[rowIndex].RightWidth.ToString("f0");
				}
				break;
			default:
				return;
			}
			this.Refresh();
		}

		private void UpdateRowGradient(int rowIndex)
		{
			SegmentData segmentData = this.m_lstSegmentData[rowIndex];
			double num = Math.Abs(segmentData.EndElevation - segmentData.StartElevation);
			double num2 = num / AssistFunc.feetToMM(segmentData.Length()) * 100.0;
			DataGridViewCell dataGridViewCell = this.dgvSegmentSInfo[6, rowIndex];
			dataGridViewCell.Value = num2.ToString("f2");
		}

		private void btnMerge_Click(object sender, EventArgs e)
		{
			List<int> list = this.SelectRowIndexList();
			if (list.Count < 2)
			{
				return;
			}
			SegmentData segmentData = null;
			for (int i = 0; i < list.Count; i++)
			{
				if (i != 0)
				{
					int index = list[i - 1];
					int index2 = list[i];
					SegmentData segmentData2 = this.m_lstSegmentData[index];
					if (segmentData != null)
					{
						segmentData2 = segmentData;
					}
					if (!SegmentData.MergeSegmentData(segmentData2, this.m_lstSegmentData[index2], ref segmentData))
					{
						return;
					}
				}
			}
			for (int i = list.Count - 1; i >= 0; i--)
			{
				this.m_lstSegmentData.RemoveAt(list[i]);
			}
			this.m_lstSegmentData.Insert(list[0], segmentData);
			CreateRampForm.PostMessage(base.Handle, 1126, IntPtr.Zero, IntPtr.Zero);
			this.Refresh();
		}

		private void dgvSegmentSInfo_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			int columnIndex = this.dgvSegmentSInfo.CurrentCell.ColumnIndex;
			if (columnIndex != 0)
			{
				if (columnIndex == 10)
				{
					int rowIndex = this.dgvSegmentSInfo.CurrentCell.RowIndex;
					double value = AssistFunc.feetToMM(this.m_lstSegmentData[rowIndex].Length());
					SubsectionForm subsectionForm = new SubsectionForm(Convert.ToInt32(value));
					if (subsectionForm.ShowDialog() == DialogResult.OK && subsectionForm.m_lstSubLength.Count > 1)
					{
						List<int> lstSubLength = subsectionForm.m_lstSubLength;
						List<SegmentData> list = new List<SegmentData>();
						SegmentData segmentData = new SegmentData(this.m_lstSegmentData[rowIndex]);
						for (int i = 0; i < lstSubLength.Count - 1; i++)
						{
							SegmentData item = null;
							SegmentData segmentData2 = null;
							SegmentData.SplitSegmentDataAtDist(segmentData, AssistFunc.mmToFeet((double)lstSubLength[i]), ref item, ref segmentData2);
							list.Add(item);
							segmentData = segmentData2;
						}
						list.Add(segmentData);
						this.m_lstSegmentData.RemoveAt(rowIndex);
						this.m_lstSegmentData.InsertRange(rowIndex, list);
						CreateRampForm.PostMessage(base.Handle, 1126, IntPtr.Zero, IntPtr.Zero);
						return;
					}
				}
				else
				{
					this.dgvSegmentSInfo.SelectionMode = DataGridViewSelectionMode.CellSelect;
					this.btnMerge.Enabled = false;
					this.Refresh();
				}
				return;
			}
			this.dgvSegmentSInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			List<int> list2 = this.SelectRowIndexList();
			if (list2.Count < 2)
			{
				this.btnMerge.Enabled = false;
				this.Refresh();
				return;
			}
			for (int j = 0; j < list2.Count; j++)
			{
				if (j != 0)
				{
					int index = list2[j - 1];
					int index2 = list2[j];
					if (!SegmentData.CanBeMerged(this.m_lstSegmentData[index], this.m_lstSegmentData[index2]))
					{
						this.btnMerge.Enabled = false;
						this.Refresh();
						return;
					}
				}
			}
			this.btnMerge.Enabled = true;
			this.Refresh();
		}

		private List<int> SelectRowIndexList()
		{
			DataGridViewSelectedRowCollection selectedRows = this.dgvSegmentSInfo.SelectedRows;
			List<int> list = new List<int>();
			for (int i = 0; i < selectedRows.Count; i++)
			{
				list.Add(selectedRows[i].Index);
			}
			list.Sort();
			return list;
		}

		private void dgvSegmentSInfo_MouseUp(object sender, MouseEventArgs e)
		{
			if (this.dgvSegmentSInfo.SelectionMode != DataGridViewSelectionMode.FullRowSelect)
			{
				if (this.dgvSegmentSInfo.CurrentCell.ColumnIndex == 11)
				{
					int rowIndex = this.dgvSegmentSInfo.CurrentCell.RowIndex;
					DataGridViewRow dataGridViewRow = this.dgvSegmentSInfo.Rows[rowIndex];
					DataGridViewCheckBoxCell dataGridViewCheckBoxCell = dataGridViewRow.Cells[11] as DataGridViewCheckBoxCell;
					if (dataGridViewCheckBoxCell != null)
					{
						SegmentData segmentData = this.m_lstSegmentData[rowIndex];
						bool flag = segmentData.VertArcRadius > 0.001;
						bool flag2 = Convert.ToBoolean(dataGridViewCheckBoxCell.Value);
						if (flag2 != flag)
						{
							double num = Convert.ToDouble(this.tbVertRampRadius.Text);
							segmentData.VertArcRadius = (flag2 ? num : 0.0);
							this.Refresh();
							return;
						}
					}
				}
			}
			else
			{
				List<int> list = this.SelectRowIndexList();
				if (list.Count < 2)
				{
					this.btnMerge.Enabled = false;
					this.Refresh();
					return;
				}
				for (int i = 1; i < list.Count; i++)
				{
					int index = list[i - 1];
					int index2 = list[i];
					if (!SegmentData.CanBeMerged(this.m_lstSegmentData[index], this.m_lstSegmentData[index2]))
					{
						this.btnMerge.Enabled = false;
						this.Refresh();
						return;
					}
				}
				this.btnMerge.Enabled = true;
				this.Refresh();
			}
		}

		private void chbSmoothTransition_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chbSmoothTransition.Checked)
			{
				this.AddSmoothTransition();
				this.tbTransitionRadius.Enabled = true;
			}
			else
			{
				this.RemoveSmoothTransition();
				this.tbTransitionRadius.Enabled = false;
			}
			this.CalculateListView();
			this.Refresh();
		}

		private void AddSmoothTransition()
		{
			for (int i = 0; i < this.m_lstSegmentData.Count - 1; i++)
			{
				SegmentData segmentData = this.m_lstSegmentData[i];
				SegmentData segmentData2 = this.m_lstSegmentData[i + 1];
				if (!segmentData.IsArc && !segmentData2.IsArc)
				{
					XYZ a = segmentData.EndPoint - segmentData.StartPoint;
					XYZ b = segmentData2.EndPoint - segmentData2.StartPoint;
					if (!Geometry.IsParallel(a, b, 1E-09))
					{
						XYZ startPoint = segmentData2.StartPoint;
						XYZ xyz = (segmentData.StartPoint - segmentData.EndPoint).Normalize();
						XYZ xyz2 = (segmentData2.EndPoint - segmentData2.StartPoint).Normalize();
						XYZ xyz3 = startPoint + xyz * 10.0;
						XYZ xyz4 = startPoint + xyz2 * 10.0;
						XYZ xyz5 = Geometry.CalculatMidPoint(xyz3, xyz4);
						XYZ xyz6 = (xyz5 - startPoint).Normalize();
						double angle = Geometry.getAngle(startPoint, xyz3);
						double angle2 = Geometry.getAngle(startPoint, xyz4);
						double a2 = Geometry.IntersectionAngle(angle, angle2) / 2.0;
						double num = Common.MMToFeet(Convert.ToDouble(this.tbTransitionRadius.Text));
						double num2 = num / Math.Sin(a2);
						XYZ xyz7 = startPoint + xyz6 * num2;
						XYZ xyz8 = Geometry.CalculateFootPoint(segmentData.StartPoint, segmentData.EndPoint, xyz7);
						this.m_lstSegmentData[i].EndPoint = xyz8;
						XYZ xyz9 = Geometry.CalculateFootPoint(segmentData2.StartPoint, segmentData2.EndPoint, xyz7);
						this.m_lstSegmentData[i + 1].StartPoint = xyz9;
						XYZ xyz10 = xyz2.CrossProduct(xyz).Normalize();
						bool isAnticlockwise = false;
						if (xyz10.IsAlmostEqualTo(XYZ.BasisZ))
						{
							isAnticlockwise = true;
						}
						double bulge = Geometry.GetBulge(xyz8, xyz9, xyz7, isAnticlockwise);
						SegmentData item = new SegmentData(xyz8, xyz9, bulge, segmentData.EndElevation, segmentData2.StartElevation, segmentData.LeftWidth, segmentData.RightWidth, 0.0);
						this.m_lstSegmentData.Insert(i + 1, item);
						i++;
					}
				}
			}
		}

		private void RemoveSmoothTransition()
		{
			for (int i = 1; i < this.m_lstSegmentData.Count - 1; i++)
			{
				SegmentData segmentData = this.m_lstSegmentData[i - 1];
				SegmentData segmentData2 = this.m_lstSegmentData[i];
				SegmentData segmentData3 = this.m_lstSegmentData[i + 1];
				if (CreateRampForm.IsTransitionSegment(segmentData, segmentData2, segmentData3))
				{
					XYZ xyz = null;
					segmentData2.Center(ref xyz);
					double num = xyz.DistanceTo(segmentData.EndPoint);
					XYZ xyz2 = XYZ.BasisZ;
					if (Geometry.LessThan(segmentData2.Bulge, 0.0))
					{
						xyz2 = -xyz2;
					}
					XYZ xyz3 = Geometry.CalculatMidPoint(segmentData2.StartPoint, segmentData2.EndPoint, xyz, xyz2);
					XYZ xyz4 = (xyz3 - xyz).Normalize();
					double angle = Geometry.getAngle(segmentData.StartPoint, segmentData.EndPoint);
					double angle2 = Geometry.getAngle(segmentData3.EndPoint, segmentData3.StartPoint);
					double a = Geometry.IntersectionAngle(angle, angle2) / 2.0;
					double num2 = num / Math.Sin(a);
					XYZ xyz5 = xyz + xyz4 * num2;
					this.m_lstSegmentData[i - 1].EndPoint = xyz5;
					this.m_lstSegmentData[i - 1].EndElevation = segmentData2.StartElevation;
					this.m_lstSegmentData[i + 1].StartPoint = xyz5;
					this.m_lstSegmentData[i + 1].StartElevation = segmentData2.EndElevation;
					this.m_lstSegmentData.RemoveAt(i);
					i--;
				}
			}
		}

		public static bool IsTransitionSegment(SegmentData previousSegData, SegmentData currentSegData, SegmentData nextSegData)
		{
			if (previousSegData.IsArc || !currentSegData.IsArc || nextSegData.IsArc)
			{
				return false;
			}
			XYZ xyz = null;
			currentSegData.Center(ref xyz);
			double val = xyz.DistanceTo(previousSegData.EndPoint);
			double val2 = xyz.DistanceTo(nextSegData.StartPoint);
			return Geometry.IsEqual(val, val2) && Geometry.IsEqual(currentSegData.StartElevation, currentSegData.EndElevation, 1.0);
		}

		private double MaxRadius()
		{
			return double.MaxValue;
		}

		private double MinRadius()
		{
			double num = 0.0;
			foreach (SegmentData segmentData in this.m_lstSegmentData)
			{
				if (Geometry.GreaterThan(segmentData.LeftWidth, num))
				{
					num = segmentData.LeftWidth;
				}
				if (Geometry.GreaterThan(segmentData.RightWidth, num))
				{
					num = segmentData.RightWidth;
				}
			}
			return num;
		}

		private void CreateRampForm_Load(object sender, EventArgs e)
		{
			this.m_bInitialized = false;
			this.ReadData();
			this.m_bInitialized = true;
		}

		private void CreateRampForm_FormClosing(object sender, FormClosingEventArgs e)
		{
            if (((System.Windows.Forms.Form)sender).DialogResult == DialogResult.OK)
			{
				if (this.chbSmoothTransition.Checked)
				{
					double minRange = this.MinRadius();
					double maxRange = this.MaxRadius();
					if (!Common.IsValidating(this.tbTransitionRadius.Text, minRange, maxRange))
					{
						string text = "\"过渡半径\"不应小于坡道宽度的一半";
						MessageBox.Show(text, "盈建科提示");
						e.Cancel = true;
						return;
					}
				}
				this.SaveData();
			}
		}

		public void ReadData()
		{
			this.GetTopBottomElevation(ref this.m_dTopElevation, ref this.m_dBottomElevation);
			this.GetLeftRightWidth(ref this.m_dLeftWidth, ref this.m_dRightWidth);
			foreach (string item in this.m_lstBoardStyles)
			{
				this.cmbBoardStyle.Items.Add(item);
			}
			if (this.m_lstBoardStyles.Count > 0)
			{
				int num = this.cmbBoardStyle.FindString(this.m_strOldFloorTypeName);
				if (num != -1)
				{
					this.cmbBoardStyle.SelectedIndex = num;
				}
				else
				{
					this.cmbBoardStyle.SelectedIndex = 0;
				}
			}
			foreach (string item2 in this.m_lstLevelNames)
			{
				this.cmbTopLevel.Items.Add(item2);
				this.cmbBottomLevel.Items.Add(item2);
			}
			if (this.m_lstLevelNames.Count > 0)
			{
				this.cmbTopLevel.SelectedIndex = this.FindLevelByElevation(this.m_dTopElevation);
				this.cmbBottomLevel.SelectedIndex = this.FindLevelByElevation(this.m_dBottomElevation);
			}
			string tempPath = Path.GetTempPath();
			string path = tempPath + "CreateRampForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.tbTopElevation.Text = (this.m_dTopElevation / 1000.0).ToString("f3");
				this.tbBottomElevation.Text = (this.m_dBottomElevation / 1000.0).ToString("f3");
				this.tbLeftWidth.Text = this.m_dLeftWidth.ToString();
				this.tbRightWidth.Text = this.m_dRightWidth.ToString();
				this.tbTransitionRadius.Text = this.m_dLeftWidth.ToString();
				this.tbVertRampRadius.Text = "20000";
				if (!this.chbSmoothTransition.Checked)
				{
					this.tbTransitionRadius.Enabled = false;
				}
				this.CalculateListView();
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			streamReader.ReadLine();
			streamReader.ReadLine();
			this.tbTopElevation.Text = (this.m_dTopElevation / 1000.0).ToString("f3");
			this.tbBottomElevation.Text = (this.m_dBottomElevation / 1000.0).ToString("f3");
			this.tbLeftWidth.Text = streamReader.ReadLine();
			this.tbRightWidth.Text = streamReader.ReadLine();
			this.tbTransitionRadius.Text = streamReader.ReadLine();
			this.chbSmoothTransition.Checked = Convert.ToBoolean(streamReader.ReadLine());
			if (!this.chbSmoothTransition.Checked)
			{
				this.tbTransitionRadius.Enabled = false;
			}
			try
			{
				string value = streamReader.ReadLine();
				this.m_bScaleSlope = Convert.ToBoolean(value);
			}
			catch
			{
				this.m_bScaleSlope = false;
			}
			this.chkSlopeUnit.Checked = this.m_bScaleSlope;
			try
			{
				this.tbVertRampRadius.Text = streamReader.ReadLine();
			}
			catch
			{
				this.tbVertRampRadius.Text = "20000";
			}
			foreach (SegmentData segmentData in this.m_lstSegmentData)
			{
				if (segmentData.VertArcRadius > 0.01)
				{
					this.tbVertRampRadius.Text = segmentData.VertArcRadius.ToString("f0");
					break;
				}
			}
			this.CalculateListView();
			streamReader.Close();
			fileStream.Close();
		}

		public static void ReadRampWidth(ref double leftWidth, ref double rightWidth)
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "CreateRampForm.dat";
			if (!File.Exists(path))
			{
				leftWidth = 1000.0;
				rightWidth = 1000.0;
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			Convert.ToInt32(streamReader.ReadLine());
			Convert.ToInt32(streamReader.ReadLine());
			streamReader.ReadLine();
			streamReader.ReadLine();
			leftWidth = Convert.ToDouble(streamReader.ReadLine());
			rightWidth = Convert.ToDouble(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "CreateRampForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.ValidatingMinusSign(this.tbTopElevation.Text));
			streamWriter.WriteLine(this.ValidatingMinusSign(this.tbBottomElevation.Text));
			streamWriter.WriteLine(this.tbLeftWidth.Text);
			streamWriter.WriteLine(this.tbRightWidth.Text);
			streamWriter.WriteLine(this.tbTransitionRadius.Text);
			streamWriter.WriteLine(this.chbSmoothTransition.Checked);
			streamWriter.WriteLine(this.chkSlopeUnit.Checked);
			streamWriter.WriteLine(this.tbVertRampRadius.Text);
			if (this.chbSmoothTransition.Checked)
			{
				this.tbTransitionRadius.Enabled = true;
			}
			else
			{
				this.tbTransitionRadius.Enabled = false;
			}
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void dgvSegmentSInfo_CurrentCellDirtyStateChanged(object sender, EventArgs e)
		{
			this.dgvSegmentSInfo.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}

		private void m_chkSlopeUnit_CheckedChanged(object sender, EventArgs e)
		{
			this.m_bScaleSlope = this.chkSlopeUnit.Checked;
			this.ReCalculateSlope();
		}

		public const int USER = 1024;

		public const int WM_SEGMENTLIST_UPDATE = 1126;

		private ExternalCommandData m_Revit;

		private List<string> m_lstBoardStyles = new List<string>();

		private List<string> m_lstLevelNames = new List<string>();

		private List<double> m_lstLevelElevations = new List<double>();

		private List<SegmentData> m_lstSegmentData = new List<SegmentData>();

		private string m_strOldFloorTypeName = "";

		private double m_dTopElevation;

		private double m_dBottomElevation;

		private double m_dLeftWidth;

		private double m_dRightWidth;

		private bool m_bInitialized;

		private bool m_bScaleSlope;

		private Previewer m_MainPreviewer;

		private Previewer m_SecotionPreviewer;

		private enum ValidatingType
		{
			None,
			GreaterThanZero,
			GreaterThanAndEqualZero,
			WidthValue,
			LevelValue,
			VertRadius
		}
	}
}
